package com.sxbbc.common.core.service.impl;

import java.util.Date;

import com.sxbbc.common.core.service.system.mode.ModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CCustomerAuthen;
import com.sxbbc.common.core.mapper.jz.CCustomerAuthenMapper;
import com.sxbbc.common.core.service.BBusinessService;
import com.sxbbc.common.core.service.ICCustomerAuthenService;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.utils.BasePage;

/**
 * <p>
 * 用户身份认证信息 服务实现类
 * </p>
 *
 * @author xiaoky
 * @since 2019-06-19
 */
@Service
public class CCustomerAuthenServiceImpl extends ServiceImpl<CCustomerAuthenMapper, CCustomerAuthen> implements ICCustomerAuthenService {
    @Autowired
    private ICCustomerService customerService;
    @Autowired
    private CCustomerAuthenMapper customerAuthenMapper;
    @Autowired
    private ModeService modeService;
    @Autowired
    private BBusinessService bBusinessService;

    @Override
    public RestResponse editAuthenApplyStatus(Integer id, Integer status, String rejectRemark) {
        CalcuUtils calcuUtils = CalcuUtils.getInstance();
        CCustomerAuthen authen = super.getById(id);
        if (status != null && calcuUtils.isEquals(authen.getStatus(), StaticUtils.STATUS_SUCCESS)) {
            return GetRest.getFail("该申请已通过，请勿重复提交");
        }
        if (status != null && calcuUtils.isEquals(authen.getStatus(), StaticUtils.STATUS_FAIL)) {
            return GetRest.getFail("该申请已驳回，请勿重复提交");
        }

        if (calcuUtils.isEquals(status, StaticUtils.STATUS_FAIL)) {
            authen.setRejectRemark(rejectRemark);
        }
        authen.setStatus(status);
        authen.setCheckTime(new Date());
        boolean result = super.updateById(authen);
        if (!result) {
            throw new ThrowJsonException("审核失败");
        }
        CCustomer customer = customerService.getById(authen.getCid());
        if (customer == null) {
            throw new ThrowJsonException("数据有误");
        }
        // 如果是通过，同步用户认证状态
        if (calcuUtils.isEquals(authen.getStatus(), StaticUtils.STATUS_SUCCESS)) {
            customer.setAuthen(StaticUtils.STATUS_YES);
            customer.setRealName(authen.getRealName());
            result = customerService.updateById(customer);
            if (!result) {
                throw new ThrowJsonException("审核失败");
            }
        } else {
            customer.setAuthen(StaticUtils.STATUS_NO);
            result = customerService.updateById(customer);
            if (!result) {
                throw new ThrowJsonException("审核失败");
            }
        }
        return GetRest.getSuccess("审核成功");
    }

    @Override
    public IPage<CCustomerAuthen> queryPage(BasePage<CCustomerAuthen> page,
                                            CCustomerAuthen authen) {
        IPage<CCustomerAuthen> pageInfo = customerAuthenMapper.queryPage(page, authen);
        return pageInfo;
    }

}
